<?php
class Business extends CI_Controller {
	function __construct() {
		parent::__construct ();
		$this->load->helper ( 'url' );
		$this->load->library ( 'form_validation' );
	}
	
	function businessList() {
		$this->load->model ( 'Mbusiness' );
		$this->load->library ( 'pagination' );
		$this->load->helper ( 'form' );
		
		if (($this->session->userdata ( 'authority' ) === '0') || ($this->session->userdata ( 'authority' ) === '1')) {
			$tmp = $this->uri->segment ( 4, 0 );
			if (strrpos ( $tmp, '_' )) {
				$data ['class1'] = substr ( $tmp, 0, strrpos ( $tmp, '_' ) );
				$data ['search'] = urldecode ( substr ( $tmp, strrpos ( $tmp, '_' ) + 1 ) );
			} else {
				$data ['search'] = '';
				$data ['class1'] = $tmp;
			}
			$data ['class2'] = $this->uri->segment ( 5, 0 );
			$data ['region'] = $this->uri->segment ( 6, 0 );
			$data ['area'] = $this->uri->segment ( 7, 0 );
			$data ['groupName'] = urldecode ( $this->uri->segment ( 11, 0 ) );
			$data ['url'] = '';
			$data ['recycleURL'] = $this->uri->segment ( 8, 0 );
			if ($data ['recycleURL'] == 0)
				$data ['recycle'] = 'N';
			elseif ($data ['recycleURL'] == 1)
				$data ['recycle'] = 'T';
			elseif ($data ['recycleURL'] == 2)
				$data ['recycle'] = 'F';
			
			$data ['stateURL'] = $this->uri->segment ( 9, 0 );
			if ($data ['stateURL'] == 0)
				$data ['state'] = 'A';
			elseif ($data ['stateURL'] == 1)
				$data ['state'] = 'N';
			elseif ($data ['stateURL'] == 2)
				$data ['state'] = 'U';
			elseif ($data ['stateURL'] == 3)
				$data ['state'] = 'P';
			elseif ($data ['stateURL'] == 4)
				$data ['state'] = 'S';
			
			$data ['showURL'] = $this->uri->segment ( 10, 0 );
			if ($data ['showURL'] == 0)
				$data ['show'] = 'N';
			elseif ($data ['showURL'] == 1)
				$data ['show'] = 'T';
			elseif ($data ['showURL'] == 2)
				$data ['show'] = 'F';
			
			if ($this->input->post ( 'class1' ) || $this->input->post ( 'class2' ) || $this->input->post ( 'region' ) || $this->input->post ( 'area' ) || $this->input->post ( 'state' ) || $this->input->post ( 'show' ) || $this->input->post ( 'recycle' ) || $this->input->post ( 'groupName' )) {
				$data ['class1'] = $this->input->post ( 'class1' );
				$data ['class2'] = $this->input->post ( 'class2' );
				$data ['region'] = $this->input->post ( 'region' );
				$data ['area'] = $this->input->post ( 'area' );
				$data ['recycle'] = $this->input->post ( 'recycle' );
				$data ['state'] = $this->input->post ( 'state' );
				$data ['show'] = $this->input->post ( 'show' );
				$data ['groupName'] = $this->input->post ( 'groupName' );
				
				if ($this->input->post ( 'search' )) {
					$tmp = preg_replace ( "/[^ " . chr ( 0x80 ) . "-" . chr ( 0xff ) . "\\w]+/ ", ' ', $this->input->post ( 'search' ) );
					$tmp = urlencode ( $tmp );
					$tmp = strtr ( $tmp, array ('+' => "%20" ) );
					redirect ( base_url () . 'admin/business/businessList/' . $data ['class1'] . '_' . $tmp . '/' . $data ['class2'] . '/' . $data ['region'] . '/' . $data ['area'] . '/' . $data ['recycleURL'] . '/' . $data ['stateURL'] . '/' . $data ['showURL'] . '/' . urlencode ( $data ['groupName'] ) );
				}
			}
			
			if ($data ['search'] != '') {
				$searchRow = explode ( ' ', $data ['search'] );
				$search = '(';
				foreach ( $searchRow as $row ) {
					if ($row) {
						$search .= "business.name like '%" . $row . "%' OR ";
						$search .= "business.groupName like '%" . $row . "%' OR ";
						$search .= "business.address like '%" . $row . "%' OR ";
						$search .= "business.phone like '%" . $row . "%' OR ";
						$search .= "business.details like '%" . $row . "%' OR ";
					}
				}
				$search = substr ( $search, 0, strlen ( $search ) - 3 );
				if ($search) {
					$search .= ')';
				}
				$tmp = urlencode ( $data ['search'] );
				$tmp = strtr ( $tmp, array ('+' => "%20" ) );
				$data ['url'] = '_' . $tmp;
			} else
				$search = '';
			
			$data ['class1List'] = $this->Mbusiness->getClass1ListAll ();
			$data ['class2List'] = $this->Mbusiness->getClass2ListAll ( $data ['class1'] );
			$data ['regionList'] = $this->Mbusiness->getRegionListAll ();
			$data ['areaList'] = $this->Mbusiness->getAreaListAll ( $data ['region'] );
			$data ['groupNameList'] = $this->Mbusiness->getGroupNameListAll ( $data );
			
			$data ['page'] = $this->uri->segment ( 12, 0 );
			$data ['num'] = $this->Mbusiness->getBusinessNumforAdmin ( $data, $search );
			
			$config ['base_url'] = base_url () . 'admin/business/businessList/' . $data ['class1'] . $data ['url'] . '/' . $data ['class2'] . '/' . $data ['region'] . '/' . $data ['area'] . '/' . $data ['recycleURL'] . '/' . $data ['stateURL'] . '/' . $data ['showURL'] . '/' . urlencode ( $data ['groupName'] );
			$config ['total_rows'] = $data ['num'];
			$config ['per_page'] = PAGE_SHOW_NUM;
			$config ['uri_segment'] = 12;
			$config ['num_links'] = 2;
			$this->pagination->initialize ( $config );
			
			$data ['businessList'] = $this->Mbusiness->getBusinessListforAdmin ( $data, $data ['page'], PAGE_SHOW_NUM, $search );
			$data ['class2List2'] = $this->Mbusiness->getClass2ListForALL ();
			$data ['form'] = array ('id' => 'businessListForm' );
			
			$data ['authority'] = $this->session->userdata ( 'authority' );
			$data ['adminName'] = $this->session->userdata ( 'adminName' );
			
			$this->load->view ( 'admin/header', $data );
			$this->load->view ( 'admin/nav' );
			$this->load->view ( 'admin/business_list' );
			$this->load->view ( 'admin/footer' );
		} else {
			if ($this->session->userdata ( 'adminName' )) {
				$data ['msg'] = '您没有管理特约商户的权限！';
				$data ['url'] = base_url () . 'admin/home';
				$this->load->view ( 'admin/admin_message', $data );
			} else {
				$data ['msg'] = '请先登录！';
				$data ['url'] = base_url () . 'admin/home/login';
				$this->load->view ( 'admin/admin_message', $data );
			}
		}
	}
	
	function add() {
		$this->load->model ( 'Mbusiness' );
		$this->load->library ( 'pagination' );
		$this->load->helper ( 'form' );
		
		if (($this->session->userdata ( 'authority' ) === '0') || ($this->session->userdata ( 'authority' ) === '1')) {
			
			$this->form_validation->set_rules ( 'name', '商户名', 'trim|required|max_length[100]' );
			$this->form_validation->set_rules ( 'details', '描述', 'trim' );
			$this->form_validation->set_rules ( 'groupName', '商户总店', 'trim|required|max_length[100]' );
			$this->form_validation->set_rules ( 'address', '地址', 'trim|max_length[100]' );
			$this->form_validation->set_rules ( 'phone', '电话', 'trim|max_length[20]' );
			$this->form_validation->set_rules ( 'class1', '分类一', 'required' );
			$this->form_validation->set_rules ( 'class2', '分类二', 'required' );
			$this->form_validation->set_rules ( 'region', '行政区', 'required' );
			$this->form_validation->set_rules ( 'area', '商区', 'required' );
			$this->form_validation->set_rules ( 'recycle', '回收', '' );
			$this->form_validation->set_rules ( 'state', '状态', '' );
			$this->form_validation->set_rules ( 'show', '展示', '' );
			
			if ($this->form_validation->run () == TRUE) {
				$data ['name'] = $this->input->post ( 'name' );
				$data ['groupName'] = $this->input->post ( 'groupName' );
				$data ['address'] = $this->input->post ( 'address' );
				$data ['phone'] = $this->input->post ( 'phone' );
				$data ['class1'] = $this->input->post ( 'class1' );
				$data ['class2'] = $this->input->post ( 'class2' );
				$data ['region'] = $this->input->post ( 'region' );
				$data ['area'] = $this->input->post ( 'area' );
				$data ['state'] = $this->input->post ( 'state' );
				$data ['logoImage'] = ' ';
				$data ['details'] = $this->input->post ( 'details' );
				if ($this->input->post ( 'show' ) === 'T')
					$data ['show'] = 'T';
				else
					$data ['show'] = 'F';
				if ($this->input->post ( 'recycle' ) === 'T')
					$data ['recycle'] = 'T';
				else
					$data ['recycle'] = 'F';
				$data ['updatePerson'] = $this->session->userdata ( 'adminName' );
				$data ['updateTime'] = date ( "Y/m/d H:i:s", time () );
				$data ['id'] = $this->Mbusiness->addBusiness ( $data );
				if ($data ['id']) {
					if ($_FILES ["userfile"] ["name"]) {
						$config ['upload_path'] = './uploads/business/';
						$config ['file_name'] = 'business_' . $data ['id'];
						$config ['allowed_types'] = 'gif|jpg|jpeg|png|bmp';
						$config ['overwrite'] = true;
						$this->load->library ( 'upload', $config );
						
						if (! $this->upload->do_upload ( 'userfile' ))
							$data ['msg'] = $this->upload->display_errors ();
						else {
							$tmp = $this->upload->data ();
							$datan ['logoImage'] = base_url () . "uploads/business/" . $tmp ['file_name'];
							$datan ['id'] = $data ['id'];
							$config ['image_library'] = 'gd2';
							$config ['source_image'] = "./uploads/business/" . $tmp ['file_name'];
							$config ['maintain_ratio'] = FALSE;
							$config ['width'] = 150;
							$config ['height'] = 70;
							$this->load->library ( 'image_lib', $config );
							
							if ($this->image_lib->resize ()) {
								if ($this->Mbusiness->updateBusiness ( $datan )) {
									$data ['msg'] = '特约商户添加成功！';
								} else {
									$data ['msg'] = '特约商户添加出错！';
								}
							} else {
								$data ['msg'] = $this->image_lib->display_errors ();
							}
						}
					} else
						$data ['msg'] = '特约商户添加成功！';
				} else {
					$data ['msg'] = '数据插入失败！';
				}
				$data ['url'] = base_url () . 'admin/business/businessList';
				$this->load->view ( 'admin/admin_message', $data );
			} else {
				$data ['class1List'] = $this->Mbusiness->getClass1ListAll ();
				$data ['regionList'] = $this->Mbusiness->getRegionListAll ();
				$data ['form'] = array ('id' => 'businessAddForm', 'enctype' => "multipart/form-data" );
				
				$data ['authority'] = $this->session->userdata ( 'authority' );
				$data ['adminName'] = $this->session->userdata ( 'adminName' );
				
				$this->load->view ( 'admin/header', $data );
				$this->load->view ( 'admin/nav' );
				$this->load->view ( 'admin/business_add' );
				$this->load->view ( 'admin/footer' );
			}
		
		} else {
			if ($this->session->userdata ( 'adminName' )) {
				$data ['msg'] = '您没有添加特约商户的权限！';
				$data ['url'] = base_url () . 'admin/home';
				$this->load->view ( 'admin/admin_message', $data );
			} else {
				$data ['msg'] = '请先登录！';
				$data ['url'] = base_url () . 'admin/home/login';
				$this->load->view ( 'admin/admin_message', $data );
			}
		}
	}
	
	function edit() {
		$this->load->model ( 'Mbusiness' );
		$this->load->library ( 'pagination' );
		$this->load->helper ( 'form' );
		
		if (($this->session->userdata ( 'authority' ) === '0') || ($this->session->userdata ( 'authority' ) === '1')) {
			$data ['id'] = $this->uri->segment ( 4, 0 );
			
			$this->form_validation->set_rules ( 'name', '商户名', 'trim|required|max_length[100]' );
			$this->form_validation->set_rules ( 'details', '描述', 'trim' );
			$this->form_validation->set_rules ( 'groupName', '商户总店', 'trim|required|max_length[100]' );
			$this->form_validation->set_rules ( 'address', '地址', 'trim|max_length[100]' );
			$this->form_validation->set_rules ( 'phone', '电话', 'trim|max_length[20]' );
			$this->form_validation->set_rules ( 'class1', '分类一', 'required' );
			$this->form_validation->set_rules ( 'class2', '分类二', 'required' );
			$this->form_validation->set_rules ( 'region', '行政区', 'required' );
			$this->form_validation->set_rules ( 'area', '商区', 'required' );
			$this->form_validation->set_rules ( 'recycle', '回收', '' );
			$this->form_validation->set_rules ( 'state', '状态', '' );
			$this->form_validation->set_rules ( 'show', '展示', '' );
			
			if ($this->form_validation->run () == TRUE) {
				$data ['name'] = $this->input->post ( 'name' );
				$data ['groupName'] = $this->input->post ( 'groupName' );
				$data ['address'] = $this->input->post ( 'address' );
				$data ['phone'] = $this->input->post ( 'phone' );
				$data ['class1'] = $this->input->post ( 'class1' );
				$data ['class2'] = $this->input->post ( 'class2' );
				$data ['region'] = $this->input->post ( 'region' );
				$data ['area'] = $this->input->post ( 'area' );
				$data ['state'] = $this->input->post ( 'state' );
				$data ['details'] = $this->input->post ( 'details' );
				if ($this->input->post ( 'show' ) === 'T')
					$data ['show'] = 'T';
				else
					$data ['show'] = 'F';
				if ($this->input->post ( 'recycle' ) === 'T')
					$data ['recycle'] = 'T';
				else
					$data ['recycle'] = 'F';
				$data ['updatePerson'] = $this->session->userdata ( 'adminName' );
				
				if ($_FILES ["userfile"] ["name"]) {
					$config ['upload_path'] = './uploads/business/';
					$config ['file_name'] = 'business_' . $data ['id'];
					$config ['allowed_types'] = 'gif|jpg|jpeg|png|bmp';
					$config ['overwrite'] = true;
					$this->load->library ( 'upload', $config );
					
					if (! $this->upload->do_upload ( 'userfile' ))
						$data ['msg'] = $this->upload->display_errors ();
					else {
						$tmp = $this->upload->data ();
						$data ['logoImage'] = base_url () . "uploads/business/" . $tmp ['file_name'];
						$config ['image_library'] = 'gd2';
						$config ['source_image'] = "./uploads/business/" . $tmp ['file_name'];
						$config ['maintain_ratio'] = FALSE;
						$config ['width'] = 150;
						$config ['height'] = 70;
						$this->load->library ( 'image_lib', $config );
						
						if ($this->image_lib->resize ()) {
							if ($this->Mbusiness->updateBusiness ( $data )) {
								$data ['msg'] = '特约商户修改成功！';
							} else {
								$data ['msg'] = '特约商户修改出错！';
							}
						} else {
							$data ['msg'] = $this->image_lib->display_errors ();
						}
					}
				} else {
					if ($this->Mbusiness->updateBusiness ( $data )) {
						$data ['msg'] = '特约商户修改成功！';
					} else {
						$data ['msg'] = '特约商户修改出错！';
					}
				}
				
				$data ['url'] = base_url () . 'admin/business/businessList';
				$this->load->view ( 'admin/admin_message', $data );
			} else {
				$data ['business'] = $this->Mbusiness->getBusinessbyId ( $data ['id'] );
				$data ['class1List'] = $this->Mbusiness->getClass1ListAll ();
				$data ['regionList'] = $this->Mbusiness->getRegionListAll ();
				$data ['form'] = array ('id' => 'businessAddForm', 'enctype' => "multipart/form-data" );
				
				$data ['authority'] = $this->session->userdata ( 'authority' );
				$data ['adminName'] = $this->session->userdata ( 'adminName' );
				
				$this->load->view ( 'admin/header', $data );
				$this->load->view ( 'admin/nav' );
				$this->load->view ( 'admin/business_edit' );
				$this->load->view ( 'admin/footer' );
			}
		
		} else {
			if ($this->session->userdata ( 'adminName' )) {
				$data ['msg'] = '您没有编辑特约商户的权限！';
				$data ['url'] = base_url () . 'admin/home';
				$this->load->view ( 'admin/admin_message', $data );
			} else {
				$data ['msg'] = '请先登录！';
				$data ['url'] = base_url () . 'admin/home/login';
				$this->load->view ( 'admin/admin_message', $data );
			}
		}
	}
	
	function delete() {
		if (($this->session->userdata ( 'authority' ) === '0') || ($this->session->userdata ( 'authority' ) === '1')) {
			$data ['id'] = $this->uri->segment ( 4, 0 );
			$this->load->model ( 'Mbusiness' );
			
			if ($this->Mbusiness->delBusinessbyId ( $data ['id'] )) {
				$data ['msg'] = '特约商户删除成功！';
			} else
				$data ['msg'] = '特约商户删除失败！';
			
			$data ['url'] = base_url () . 'admin/business/businessList';
			$this->load->view ( 'admin/admin_message', $data );
		} else {
			if ($this->session->userdata ( 'adminName' )) {
				$data ['msg'] = '您没有删除特约商户的权限！';
				$data ['url'] = base_url () . 'admin/home';
				$this->load->view ( 'admin/admin_message', $data );
			} else {
				$data ['msg'] = '请先登录！';
				$data ['url'] = base_url () . 'admin/home/login';
				$this->load->view ( 'admin/admin_message', $data );
			}
		}
	}
	
	function upload() {
		if ($this->session->userdata ( 'authority' ) === '0' || ($this->session->userdata ( 'authority' ) === '1')) {
			if ($this->input->post ()) {
				$this->load->model ( 'Mbusiness' );
				$class1 = $this->Mbusiness->getClass1ListAll ();
				$class2 = $this->Mbusiness->getClass2ListforHome ();
				$region = $this->Mbusiness->getRegionListAll ();
				$area = $this->Mbusiness->getAreaListAll2 ();
				
				$config ['upload_path'] = './uploads/business/';
				$config ['allowed_types'] = 'xls|xlsx|csv';
				$config ['overwrite'] = true;
				$this->load->library ( 'upload', $config );
				
				if (! $this->upload->do_upload ( 'userfile' )) {
					$error ['msg'] = $this->upload->display_errors ();
				} else {
					$data = $this->upload->data ();
					$filePath = './uploads/business/' . $data ['file_name'];
					
					require_once 'application/libraries/PHPExcel.php';
					require_once 'application/libraries/PHPExcel/IOFactory.php';
					require_once 'application/libraries/PHPExcel/Reader/Excel5.php';
					
					$PHPExcel = new PHPExcel ();
					$PHPReader = new PHPExcel_Reader_Excel2007 ();
					if (! $PHPReader->canRead ( $filePath )) {
						$PHPReader = new PHPExcel_Reader_Excel5 ();
						if (! $PHPReader->canRead ( $filePath )) {
							$PHPReader = new PHPExcel_Reader_CSV ();
							if (! $PHPReader->canRead ( $filePath )) {
								$error ['msg'] = '不能读取文件' . $filePath;
								$error ['url'] = base_url () . 'admin/business/upload';
								$this->load->view ( 'admin/admin_message', $error );
								return;
							}
						}
					}
					
					$PHPExcel = $PHPReader->load ( $filePath );
					$currentSheet = $PHPExcel->getActiveSheet ();
					$allRow = $currentSheet->getHighestRow ();
					if ($currentSheet->getCell ( "C2" )->getValue () == '总店名称') {
						for($j = 3; $j <= $allRow; $j ++) {
							$str = '';
							for($k = 'A'; $k <= 'Q'; $k ++) {
								$str .= $currentSheet->getCell ( "$k$j" )->getCalculatedValue () . '\\';
							}
							
							$strs = explode ( "\\", $str );
							if ($strs [0] == NULL)
								break;
							$mdata ['groupName'] = $strs [2];
							$mdata ['name'] = $strs [3];
							$mdata ['address'] = $strs [4];
							$mdata ['phone'] = $strs [5];
							foreach ( $class1 as $row ) {
								if ($row ['name'] == $strs [7]) {
									$mdata ['class1'] = $row ['id'];
									break;
								}
							}
							if ($strs [7] == $strs [8])
								$mdata ['class2'] = 0;
							else {
								foreach ( $class2 as $row ) {
									if ($row ['name'] == $strs [8]) {
										$mdata ['class2'] = $row ['id'];
										break;
									}
								}
							}
							foreach ( $region as $row ) {
								if ($row ['name'] == $strs [9] . '区' || $row ['name'] == $strs [9] . '新区' || $row ['name'] == $strs [9]) {
									$mdata ['region'] = $row ['id'];
									break;
								}
							}
							foreach ( $area as $row ) {
								if ($row ['name'] == $strs [10]) {
									$mdata ['area'] = $row ['id'];
									break;
								}
							}
							if ($strs [11] == '不可以')
								$mdata ['recycle'] = 'F';
							elseif ($strs [11] == '可以')
								$mdata ['recycle'] = 'T';
							$mdata ['details'] = $strs [13];
							if ($strs [12] == '正常')
								$mdata ['state'] = 'N';
							elseif ($strs [12] == '即将开通')
								$mdata ['state'] = 'U';
							elseif ($strs [12] == '暂停使用')
								$mdata ['state'] = 'P';
							elseif ($strs [12] == '停用')
								$mdata ['state'] = 'S';
							$mdata ['logoImage'] = ' ';
							$mdata ['show'] = 'F';
							$mdata ['updateTime'] = date ( "Y/m/d H:i:s", time () );
							$mdata ['updatePerson'] = $this->session->userdata ( 'adminName' );
							
							$bid = $this->Mbusiness->addBusiness ( $mdata );
							if (! $bid) {
								$error ['msg'] = '数据库写入失败！';
								$error ['url'] = base_url () . 'admin/business/upload';
								$this->load->view ( 'admin/admin_message', $error );
								return;
							}
						}
						$error ['msg'] = '导入成功！';
					} else
						$error ['msg'] = 'Excel模板错误，请检查是否选择了正确的文件。';
				}
				$error ['url'] = base_url () . 'admin/business/businessList';
				$this->load->view ( 'admin/admin_message', $error );
			} else {
				$data ['authority'] = $this->session->userdata ( 'authority' );
				$data ['adminName'] = $this->session->userdata ( 'adminName' );
				$this->load->view ( 'admin/header', $data );
				$this->load->view ( 'admin/nav' );
				$this->load->view ( 'admin/business_upload' );
				$this->load->view ( 'admin/footer' );
			}
		} else {
			if ($this->session->userdata ( 'adminName' )) {
				$data ['msg'] = '您没有导入特约商户的权限！';
				$data ['url'] = base_url () . 'admin/home';
				$this->load->view ( 'admin/admin_message', $data );
			} else {
				$data ['msg'] = '请先登录！';
				$data ['url'] = base_url () . 'admin/home/login';
				$this->load->view ( 'admin/admin_message', $data );
			}
		}
	}
	
	function uploadLogo() {
		$this->load->library ( 'pagination' );
		
		$data ['search'] = '';
		$search = '';
		
		if ($this->input->post ( 'search' )) {
			$data ['search'] = preg_replace ( "/[^ " . chr ( 0x80 ) . "-" . chr ( 0xff ) . "\\w]+/ ", ' ', $this->input->post ( 'search' ) );
			if ($data ['search'] != '') {
				$searchRow = explode ( ' ', $data ['search'] );
				$search = '(';
				foreach ( $searchRow as $row ) {
					if ($row) {
						$search .= "groupName like '%" . $row . "%' OR ";
					}
				}
				$search = substr ( $search, 0, strlen ( $search ) - 3 );
				if ($search)
					$search .= ')';
			}
		}
		
		if ($this->session->userdata ( 'authority' ) === '0' || ($this->session->userdata ( 'authority' ) === '1')) {
			$this->load->model ( 'Mbusiness' );
			$data ['msg'] = '';
			
			if ($this->input->post ('groupName')) {
				$data ['groupName'] = $this->input->post ( 'groupName' );
				$datan ['updatePerson'] = $this->session->userdata ( 'adminName' );
				$datan ['updateTime'] = date ( "Y/m/d H:i:s", time () );
				if ($this->input->post ( 'show' ) === 'T')
					$datan ['show'] = 'T';
				else
					$datan ['show'] = 'F';
				
				if ($_FILES ["userfile"] ["name"]) {
					$config ['upload_path'] = './uploads/business/';
					$config ['file_name'] = 'business_' . $this->input->post ( 'id' );
					$config ['allowed_types'] = 'gif|jpg|jpeg|png|bmp';
					$config ['overwrite'] = true;
					$this->load->library ( 'upload', $config );
					
					if (! $this->upload->do_upload ( 'userfile' ))
						$data ['msg'] = $this->upload->display_errors ();
					else {
						$tmp = $this->upload->data ();
						$datan ['logoImage'] = base_url () . "uploads/business/" . $tmp ['file_name'];
						
						$config3 ['image_library'] = 'gd2';
						$config3 ['source_image'] = "./uploads/business/" . $tmp ['file_name'];
						$config3 ['maintain_ratio'] = FALSE;
						$config3 ['width'] = 150;
						$config3 ['height'] = 70;
						$this->load->library ( 'image_lib', $config3 );
						
						if (! $this->image_lib->resize ()) {
							$data ['msg'] = $this->image_lib->display_errors ();
						}
					}
				}
				if ($this->Mbusiness->updateLogo ( $datan, $data ['groupName'] )) {
					$data ['msg'] .= '添加成功！';
				} else {
					$data ['msg'] .= '添加出错！';
				}
			}
			
			$data ['page'] = $this->uri->segment ( 4, 0 );
			$data ['num'] = $this->Mbusiness->getGroupLogoNum ( $search );
			
			$config2 ['base_url'] = base_url () . 'admin/business/uploadLogo/';
			$config2 ['total_rows'] = $data ['num'];
			$config2 ['per_page'] = PAGE_SHOW_NUM;
			$config2 ['uri_segment'] = 4;
			$config2 ['num_links'] = 2;
			$this->pagination->initialize ( $config2 );
			$data ['list'] = $this->Mbusiness->getGroupLogoList ( $data ['page'], PAGE_SHOW_NUM, $search );
			
			$data ['authority'] = $this->session->userdata ( 'authority' );
			$data ['adminName'] = $this->session->userdata ( 'adminName' );
			$this->load->view ( 'admin/header', $data );
			$this->load->view ( 'admin/nav' );
			$this->load->view ( 'admin/business_uploadLogo' );
			$this->load->view ( 'admin/footer' );
		
		} else {
			if ($this->session->userdata ( 'adminName' )) {
				$data ['msg'] = '您没有编辑特约商户的权限！';
				$data ['url'] = base_url () . 'admin/home';
				$this->load->view ( 'admin/admin_message', $data );
			} else {
				$data ['msg'] = '请先登录！';
				$data ['url'] = base_url () . 'admin/home/login';
				$this->load->view ( 'admin/admin_message', $data );
			}
		}
	}
	
	// 功能函数
	function getClassT() {
		$id = $this->input->get ( 'id' );
		$data ['ret'] = 0;
		if ($id > 0) {
			$this->load->model ( 'Mbusiness' );
			$data ['class2List'] = $this->Mbusiness->getClass2List ( $id, 0, '', 0, 0, 0 );
			if ($data ['class2List'])
				$data ['ret'] = 1;
		}
		
		echo json_encode ( $data );
	}
	
	function getAreaT() {
		$id = $this->input->get ( 'id' );
		$data ['ret'] = 0;
		if ($id > 0) {
			$this->load->model ( 'Mbusiness' );
			$data ['areaList'] = $this->Mbusiness->getAreaList ( $id, 0, '', 0, 0, 0 );
			if ($data ['areaList'])
				$data ['ret'] = 1;
		}
		
		echo json_encode ( $data );
	}
	
	function getClassTA() {
		$id = $this->input->get ( 'id' );
		$data ['ret'] = 0;
		if ($id > 0) {
			$this->load->model ( 'Mbusiness' );
			$data ['class2List'] = $this->Mbusiness->getClass2ListAll ( $id );
			if ($data ['class2List'])
				$data ['ret'] = 1;
		}
		
		echo json_encode ( $data );
	}
	
	function getAreaTA() {
		$id = $this->input->get ( 'id' );
		$data ['ret'] = 0;
		if ($id > 0) {
			$this->load->model ( 'Mbusiness' );
			$data ['areaList'] = $this->Mbusiness->getAreaListAll ( $id );
			if ($data ['areaList'])
				$data ['ret'] = 1;
		}
		
		echo json_encode ( $data );
	}

}